<?php
/*
 * ConnectDB class to make mysql queries (insert, select, delete)
 */
class ConnectDB
	{
				
		private static function connect_to_db()
		{
			$server = 'localhost';
			$user = 'root';
			$password = ''; //pending
			$database = 'icopdb';
			$connection = mysql_connect($server, $user, $password);
			
			if(!$connection)
			{
				throw new Exception('Could not connect to DB.');
			}
			
			mysql_select_db($database, $connection);
			
			return $connection;
		}
		
				
		private static function close_connection($connection)
		{
			mysql_close($connection);
		}
		
		public static function run_query($strQuery, $getId=FALSE)
		{
			try
			{
				$connection = self::connect_to_db();
				
				$result = $getId ? array(0 => mysql_query($strQuery), 1 => mysql_insert_id($connection)) : mysql_query($strQuery);
				
				self::close_connection($connection);
				
				return $result;
			} catch(Exception $e)
			{
				echo 'Caught exception: ', $e->getMessage(), "\n";
			}
		}
		
		public static function connect_to_db_i()
		{
			$server = 'localhost';
			$user = 'master';
			$password = 'admin';
			$db = 'materias';
			//echo $server.' '.$user.' '.$password.' '.$db.'<br>';
			$connection = mysqli_connect($server, $user, $password,$db);
			
			if(!$connection)
			{
				throw new Exception('Could not connect to DB.');
			}
			
			return $connection;
		}
		
				
		public static function close_connection_i($connection)
		{
			mysqli_close($connection);
		}
		
		public static function run_query_i($strQuery)
		{
			try
			{
				$connection = self::connect_to_db_i();
				
				$result = mysqli_query($strQuery);
			
				self::close_connection_i($connection);
				
				return $result;
			} catch(Exception $e)
			{
				echo 'Caught exception: ', $e->getMessage(), "\n";
			}
		}
}
?>